package com.hjduan.spring.cloud.request.service;

import com.hjduan.spring.cloud.request.model.ArticleEntity;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangheng on 2017/10/15.
 */
@Service
public class ArticleService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod="selfException")
    public Map<String, Object> getArticleList(Map<String, Object> map){

        return restTemplate.postForObject("http://article/article/list", map, Map.class);
    }

    public Map<String, Object> selfException(Map<String, Object> map){
        System.out.println("There is a exception===="+map.get("status"));
        return null;
    }

    @HystrixCommand(fallbackMethod="getObjectException")
    public Map<String, Object> getArticle(Long id){
        return restTemplate.getForObject("http://article/article/info/"+id, Map.class);
    }

    public Map getObjectException(Long id){
        System.out.println("there is an Exception ====="+id);
        return null;
    }


    @HystrixCommand(fallbackMethod="SaveException")
    public Map<String, Object> saveArticle(ArticleEntity articleEntity){
        return restTemplate.postForObject("http://article/article/save",articleEntity,Map.class);
    }

    public Map SaveException(ArticleEntity articleEntity){
        System.out.println("there is an Exception ====="+articleEntity.getContent());
        return null;
    }

}
